"""
@author:王耀
@date:2021/9/17
"""

import numpy as np
import cv2

def Gray(img):
    """
    作用：将彩色图转换为灰度图像
    img：输入的图片

    返回：原图的灰度图
    """
    if img is None:
        raise Exception('input image ERROR!')
    if len(img.shape) == 2:
        return img
    if len(img.shape) != 3:
        raise Exception('Not RBG image')

    img = img.mean(axis=2)
    img = np.clip(img, 0, 255)
    img = img.astype(np.uint8)

    print('执行完毕：转为灰度图')

    return img

#########################################
# if __name__ == '__main__':
#     img = cv2.imread('test.jpg')
#     cv2.imshow('',img)
#     cv2.waitKey()
#
#     result_img = Gray(img)
#     cv2.imshow('',result_img)
#     cv2.waitKey()